package com.zhouyu.gamecommunity.entity;

import com.zhouyu.gamecommunity.contants.ResultCode;
import lombok.Data;

import java.io.Serializable;

/**
 * 通用返回结果封装类
 * @param <T> 数据泛型
 */
@Data
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    
    // 状态码
    private Integer code;
    
    // 返回信息
    private String message;
    
    // 返回数据
    private T data;
    
    // 时间戳
    private Long timestamp = System.currentTimeMillis();

    // 成功静态方法
    public static <T> Result<T> success() {
        return success(null);
    }
    
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }
    
    // 失败静态方法
    public static <T> Result<T> fail() {
        return fail(ResultCode.FAILED);
    }
    
    public static <T> Result<T> fail(ResultCode resultCode) {
        return fail(resultCode.getCode(), resultCode.getMessage());
    }

    public static <T> Result<T> fail(String message) {
        return fail(ResultCode.FAILED.getCode(), message);
    }
    
    public static <T> Result<T> fail(Integer code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }
    
    // 链式调用方法
    public Result<T> code(Integer code) {
        this.code = code;
        return this;
    }
    
    public Result<T> message(String message) {
        this.message = message;
        return this;
    }
    
    public Result<T> data(T data) {
        this.data = data;
        return this;
    }
    
    // getter和setter省略...
}